class_name Collectible
extends Area2D


enum Type {
	KNIFE, GUN, FOOD
}


@onready var animationPlayer: AnimationPlayer = $AnimationPlayer
@onready var sprite: Sprite2D = $CollectibleSprite
@onready var damageEmitter: Area2D = $DamageEmitter
@onready var stateMachine: NodeStateMachine = $StateMachine


@export var type: Type = Type.KNIFE
@export var damage: int
@export var speed: float
# 掉落时的向上抛出的强度，越大抛出的越高
@export var knockdown_intensity: float

# 销毁前消失的速度，仅在设置了自动销毁时落地后执行消失
@export var dissipate_speed: float = 3.0


const STATE_FALL := "fall"
const STATE_GROUNDED := "grounded"
const STATE_FLY := "fly"


const ANIMATION_FALL := "fall"
const ANIMATION_GROUNDED := "grounded"
const ANIMATION_FLY := "fly"


var direction: Vector2 = Vector2.ZERO
var initial_state: String
var height: float = 0.0
var auto_destroy: bool


func _ready():
	if initial_state != null:
		await get_tree().process_frame
		stateMachine.transition_to(initial_state)


func flip_sprite():
	if direction.x > 0:
		sprite.flip_h = false
	elif direction.x < 0:
		sprite.flip_h = true


# 若设置了自动销毁，则会先逐渐消失后再销毁
func process_auto_destroy(delta : float):
	if not auto_destroy:
		return
	modulate.a -= delta * dissipate_speed
	if modulate.a < 0.001:
		queue_free()
